// src/main/java/com/xxxy/ljh/myapplication/activity/SettingsActivity.java
package com.xxxy.ljh.myapplication.activity.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import com.xxxy.ljh.myapplication.R;
import com.xxxy.ljh.myapplication.activity.login.LoginActivity;

public class SettingsActivity extends AppCompatActivity {

    private Button btnEditProfile;
    private Button btnSwitchTheme;
    private Button btnCheckUpdate;
    
    private SharedPreferences prefs;
    private boolean isNightMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        initViews();
        setupSharedPreferences();
        setupClickListeners();
    }

    private void initViews() {
        btnEditProfile = findViewById(R.id.btn_edit_profile);
        btnSwitchTheme = findViewById(R.id.btn_switch_theme);
        btnCheckUpdate = findViewById(R.id.btn_check_update);
    }

    private void setupSharedPreferences() {
        prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
        isNightMode = prefs.getBoolean("night_mode", false);
    }

    private void setupClickListeners() {
        // 修改个人信息
        btnEditProfile.setOnClickListener(v -> {
            // 这里可以跳转到个人信息编辑页面
            Toast.makeText(SettingsActivity.this, "修改个人信息功能待实现", Toast.LENGTH_SHORT).show();
        });

        // 切换模式
        btnSwitchTheme.setOnClickListener(v -> switchTheme());

        // 检查更新
        btnCheckUpdate.setOnClickListener(v -> {
            Toast.makeText(SettingsActivity.this, "当前已是最新版本", Toast.LENGTH_SHORT).show();
        });

    }

    private void switchTheme() {
        isNightMode = !isNightMode;
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("night_mode", isNightMode);
        editor.apply();

        if (isNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }

        Toast.makeText(SettingsActivity.this, 
            isNightMode ? "已切换至夜间模式" : "已切换至日间模式", 
            Toast.LENGTH_SHORT).show();
    }

    private void logout() {
        // 清除登录状态
        SharedPreferences userPrefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
        userPrefs.edit()
                .putBoolean("is_logged_in", false)
                .remove("username")
                .remove("user_id")
                .apply();

        // 跳转到登录页面
        Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
        
        Toast.makeText(SettingsActivity.this, "已退出登录", Toast.LENGTH_SHORT).show();
    }
}
